<?php
namespace Wx\Controller;
use Think\Controller;

class StoresController extends Controller{
	//门店首页http://www.xiangtws.com/Wx/Stores/s_id//
	public function index(){
		$s_id=$_GET['s_id'];//门店id
		if($s_id>0){
			//门店信息
			$shop_store=D('shop_store');
			$shopinfo=$shop_store->where("id=".$s_id)->find();
			$this->assign('shopinfo',$shopinfo);
			//社区店铺兑换
			$exchange_goods=D('exchange_goods');
			$shop_store=D('shop_store');
			$s_u_id=$shop_store->where("id=".$s_id)->getField('u_id');
			$where="";
			if($s_u_id>0)$where=" or u_id=".$s_u_id;
			$goods=$exchange_goods->where("u_id=0".$where)->select();//品牌类型兑换产品
			$goods_1='';//纯积分
			$goods_2='';//积分+现金
			$goods_3='';//纯现金
					$category=D('category');
			foreach($goods as $i=>$one){
				//u_id=0的判断本店是否在授权范围内
				$c_id_arr=explode(',',$one['c_id_str']);
				if(($one['u_id']==0 && in_array($shopinfo['c_id'],$c_id_arr))||$one['u_id']>0){
					//本小区在该品牌兑换内|兑换是本社区发布 
				}else{
					continue;
				}//end if
				if($one['state']!=1)continue;//只展示上架产品
					//产品第一图片 
					$img=explode(',',$one['head_img']);
					$goods[$i]['f_img']=$img[0];//第一张图片作为封面图
					//查商品单位
					$getc=$category->where("id=".$one['unit'])->find();
					$goods[$i]['unit']=$getc['title'];
				if($one['level_cash_1']==0 and $one['level_cash_2']==0 and $one['level_cash_3']==0 and $one['level_cash_4']==0 ){
					$goods_1[]=$goods[$i];
				}else if($one['level_integral_1']==0 and $one['level_integral_2']==0 and $one['level_integral_3']==0 and $one['level_integral_4']==0 ){
					$goods_3[]=$goods[$i];
				}else{
					$goods_2[]=$goods[$i];
				}
				
			}//end foreach
			$this->assign('goods_integral',$goods_1);
			$this->assign('goods_int_ca',$goods_2);
			$this->assign('goods_cash',$goods_3);
			//该社区拼团
			//即将成团（本小区）————————————————————————————————
			$s_c_id=$shop_store->where("id=".$s_id)->getField('c_id');
//			dump($s_c_id);
			$shop_spellgroup_copy=D('shop_spellgroup_copy');
			$shop_spellgroup=D('shop_spellgroup');
			$category=D('category');
			$member=D('member');
			$member_farmers=D('member_farmers');
			if($s_c_id>0){
			    // 门店拼团-未开团商品
                $group1 = $shop_spellgroup->where("s_id = {$s_id} and state = 1")->order('id desc')->select();
                foreach ($group1 as $k=>$v){
                    $re = $shop_spellgroup_copy->where("sg_id = {$v['id']}")->find();
                    if($re){
                        unset($group1[$k]);
                    }else{
                        //产品第一图片
                        $img=explode(',',$v['h_imgs']);
                        $group1[$k]['f_img'] = $img[0];//第一张图片作为封面图
                        // 产品单位
                        $group1[$k]['unit'] = $category->where("id=".$v['unit'])->getField('title');
                        // 发布人信息
                        $group1[$k]['f_avatar'] = $member->where('id='.$v['u_id'])->getField('avatar');
                        $group1[$k]['f_realname'] = $member->where('id='.$v['u_id'])->getField('realname');
                    }
                }
                // 已开团
                $getneedgood1 = $shop_spellgroup_copy->table(C('DB_PREFIX')."shop_spellgroup_copy as sc")
                                ->join(C('DB_PREFIX')."shop_spellgroup as sg ON sg.id=sc.sg_id")
                                ->join(C('DB_PREFIX')."member as me ON me.id = sg.u_id")
                                ->where("sg.state = 1 and sc.state <>2 and sc.c_id = {$s_c_id}")
                                ->field("sg.*,me.avatar as f_avatar,me.realname as f_realname,sc.num as copy_num,sc.id as copy_id")
                                ->order('sc.id desc')
                                ->select();

                foreach($getneedgood1 as $k=>$v){
                    //产品第一图片
                    $img=explode(',',$v['h_imgs']);
                    $getneedgood1[$k]['f_img'] = $img[0];//第一张图片作为封面图
                    // 产品单位
                    $getneedgood1[$k]['unit'] = $category->where("id=".$v['unit'])->getField('title');
                    //村庄
                    $getneedgood[$k]['village']=$member_farmers->where("u_id=".$v['u_id'])->getField('village');
                    $getneedgood1[$k]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$v['id'])->sum('num');//已售多少份
                    $getneedgood1[$k]['neednum']=$v['min_num']-$one['copy_num'];
                }

                $this->assign('group1',$group1);
                $this->assign('needgoodarr',$getneedgood1);


//				$getneedgood=$shop_spellgroup_copy->where("orsuccess=0 and state=0 and c_id=".$s_c_id)->select();
//				foreach($getneedgood as $k=>$one){
//					$getgood=$shop_spellgroup->where("id=".$one['sg_id'])->find();
//					//首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名、已售多少份、还差多少成团
//					//产品第一图片
//					$img=explode(',',$getgood['h_imgs']);
//					$getneedgood[$k]['f_img']=$img[0];//第一张图片作为封面图
//					$getneedgood[$k]['type']=$getgood['type'];
//					$getneedgood[$k]['title']=$getgood['title'];
//					$getneedgood[$k]['u_num']=$getgood['u_num'];
//					$getu=$category->where("id=".$getgood['unit'])->find();
//					$getneedgood[$k]['unit']=$getu['title'];
//					$getneedgood[$k]['actual_price']=$getgood['actual_price'];
//					//发布人头像、姓名
//					$shop_spellgroup=D('shop_spellgroup');
//					$getsg=$shop_spellgroup->where("id=".$one['sg_id'])->find();
//					$getinfo=$member->where("id=".$getsg['u_id'])->find();
//					$getneedgood[$k]['f_realname']=$getinfo['realname'];
//					$getneedgood[$k]['f_avatar']=$getinfo['avatar'];
//					//村庄
//					$getneedgood[$k]['village']=$member_farmers->where("u_id=".$getsg['u_id'])->getField('village');
//
//					$getneedgood[$k]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['sg_id'])->sum('num');//已售多少份
//					$getneedgood[$k]['neednum']=$getgood['min_num']-$one['num'];
//				}//end foreach
//				$this->assign('needgoodarr',$getneedgood);

			}//end if
			//即将成团（本小区）————————————————————————————————end
			$this->display('house');
		}else{
			$this->error('非法操作！','/Wx/Index/',2);
		}//end if
		
	}//end fun
	
	
}//end class